﻿using System;
using System.Windows.Forms;

namespace Administracja
{
    public partial class OsobyWplatyEdycja : Form
    {

        private readonly int idWp;

        public OsobyWplatyEdycja(int idWp)
        {
            InitializeComponent();
            this.idWp = idWp;
            Wyswietl();
        }

        private void Wyswietl()
        {
            id_l.Text = "ID: " + idWp;

            var reader1D = Baza.Wczytaj1D("SELECT * FROM WPLATY WHERE ID = '" + idWp + "'");

            nr_fak_l.Text = "Nr. faktury: " + Baza.Wczytaj("SELECT Numer FROM FAKTURY WHERE ID = '" + reader1D[1] + "'");
            data_wplaty.Value = DateTime.Parse(reader1D[2]);
            wplata_box.Text = Convert.ToDouble(reader1D[3]).ToString("0.00");
            odsetki_box.Text = Convert.ToDouble(reader1D[4]).ToString("0.00");
            if (reader1D[5] == "P") przelew_rad.Checked = true;
            if (reader1D[6] == "KP") kp_rad.Checked = true;
            numer_dok_box.Text = reader1D[7];
        }

        private void zapisz_Click(object sender, EventArgs e)
        {
            string dokument;

            string sposob = gotowka_rad.Checked ? "G" : "P";

            if (fv_rad.Checked) dokument = "FV";
            else
            {
                if (kp_rad.Checked) dokument = "KP";
                else dokument = "W";
            }

            Baza.Zapisz("UPDATE WPLATY SET "
                + "Data = '" + data_wplaty.Value.Day + "." + data_wplaty.Value.Month + "." + data_wplaty.Value.Year
                + "', Kwota = '" + wplata_box.Text.Replace(",", ".")
                + "', Odsetki = '" + odsetki_box.Text.Replace(",", ".")
                + "', Sposob = '" + sposob
                + "', Dokument = '" + dokument
                + "', Numer_Dok = '" + numer_dok_box.Text
                + "' WHERE ID = " + idWp + ";");

            Close();
        }

        private void anuluj_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void gotowka_rad_CheckedChanged(object sender, EventArgs e)
        {
            if (gotowka_rad.Checked)
            {
                fv_rad.Enabled = true;
                kp_rad.Enabled = true;
                wyciag_rad.Enabled = false;
                fv_rad.Checked = true;
            }
        }

        private void przelew_rad_CheckedChanged(object sender, EventArgs e)
        {
            if (przelew_rad.Checked)
            {
                fv_rad.Enabled = false;
                kp_rad.Enabled = false;
                wyciag_rad.Enabled = true;
                wyciag_rad.Checked = true;
            }
        }

        private void wplata_box_Click(object sender, EventArgs e)
        {
            wplata_box.SelectAll();
        }

        private void odsetki_box_Click(object sender, EventArgs e)
        {
            odsetki_box.SelectAll();
        }

        private void numer_dok_box_Click(object sender, EventArgs e)
        {
            numer_dok_box.SelectAll();
        }
    }
}
